﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using Telerik.Web.UI;

namespace KPIS.GERP.GHRMS.APP.App_UI.Forms
{
    public partial class PR_SalaryPromotionConfig : System.Web.UI.Page
    {
        private SsMng ssMng = new SsMng();
        SystemYearTypeBLL yearTypeBLL = new SystemYearTypeBLL();
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {             
                bindEmpType();
                bindYearCombo();
                LoadDataGridOverload();
            }
            
        }
        
        private void LoadDataGrid()
        {
            try
            {
                SalaryPromotionConfigBLL obj = new SalaryPromotionConfigBLL();

                DataTable dt = null;
                int prm_year = int.Parse(cbYear.SelectedValue);
                int emp_type_seq = int.Parse(cbEmployeeType.SelectedValue);



                dt = obj.selectPromotions(prm_year, emp_type_seq);   

                RadGrid2.DataSource = dt;
                RadGrid2.DataBind();
            }
            catch (Exception ex)
            {

            }
        }

        public void bindYearCombo()
        {
            int curYear = int.Parse(DateTime.Today.Year.ToString());
            curYear += 543;
            int MinYear = curYear - 10;
            int MaxYear = curYear + 10;
            DataTable dt = new DataTable();
            DataColumn dc = dt.Columns.Add("Name");

            for (int i = MinYear; i <= MaxYear; i++)
            {
                dt.Rows.Add(Convert.ToString(i));
            }
            cbYear.DataSource = dt;
            cbYear.DataBind();
            cbYear.SelectedValue = curYear.ToString();
            cbYear_SelectedIndexChanged(null, null);
        }


        private void LoadDataGridOverload()
        {
            try
            {

                if(cbEmployeeType.SelectedIndex == -1)
                    return ;

                if (cbYear.SelectedIndex == -1)
                    return;

                SalaryPromotionConfigBLL obj = new SalaryPromotionConfigBLL();

                int prm_year,emp_type_seq;

                prm_year = int.Parse(cbYear.SelectedValue);
                emp_type_seq = int.Parse(cbEmployeeType.SelectedValue);   

                DataTable dt = null;

            
            
            dt = obj.selectPromotions(prm_year, emp_type_seq);   

                RadGrid2.DataSource = dt;
                RadGrid2.DataBind();
            }
            catch (Exception ex)
            {

            }
        }



        protected void RadGrid2_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                SalaryPromotionConfigBLL obj = new SalaryPromotionConfigBLL();
                int seq = Convert.ToInt32((e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["prm_seq"]);
                obj.Delete(seq, ssMng.EmpSeqI);
            }
            LoadDataGrid();
        }


        private void bindEmpType()
        {
            PromotionDeptBLL obj = new PromotionDeptBLL();
            cbEmployeeType.DataSource = obj.GetEmployeeTypes();
            cbEmployeeType.DataTextField = "EMP_TYPE_NAME";
            cbEmployeeType.DataValueField = "EMP_TYPE_SEQ";
            cbEmployeeType.DataBind();
            cbEmployeeType.SelectedIndex = 0;
            cbEmployeeType_SelectedIndexChanged(null, null);
        }

        protected void cmdSearch_Click(object sender, EventArgs e)
        {
            LoadDataGridOverload();
        }

        protected void cbEmployeeType_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            try
            {
                DataSet ds = yearTypeBLL.GetYearType(int.Parse(cbEmployeeType.SelectedValue));
                lblTypeYear.Text = "ปี :" + ds.Tables[0].Rows[0]["year_type_name"].ToString();
                int myYearNow = DateTime.Today.Year;
         
            }
            catch (Exception ex)
            {
                lblTypeYear.Text = "";
            }

            LoadDataGridOverload();
        }

        protected void cbYear_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            LoadDataGridOverload();
        }

        protected void RadGrid2_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        { 
            //
        }

        protected void RadGrid2_GroupsChanging(object sender, GridGroupsChangingEventArgs e)
        {
            //
        }

        protected void RadGrid2_ItemDataBound(object sender, GridItemEventArgs e)
        {
            //
        }
    }
}
